{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "def factorize(number):\n",
    "    for i in range(1, number + 1):\n",
    "        if number % i == 0:\n",
    "            yield i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Took 0.973 seconds\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "from time import time\n",
    "numbers = [2139079, 1214759, 1516637, 1852285]\n",
    "start = time()\n",
    "for number in numbers:\n",
    "    list(factorize(number))\n",
    "end = time()\n",
    "print('Took %.3f seconds' % (end - start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "from threading import Thread\n",
    "\n",
    "class FactorizeThread(Thread):\n",
    "    def __init__(self, number):\n",
    "        super().__init__()\n",
    "        self.number = number\n",
    "\n",
    "    def run(self):\n",
    "        self.factors = list(factorize(self.number))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 4\n",
    "start = time()\n",
    "threads = []\n",
    "for number in numbers:\n",
    "    thread = FactorizeThread(number)\n",
    "    thread.start()\n",
    "    threads.append(thread)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Took 0.920 seconds\n"
     ]
    }
   ],
   "source": [
    "# Example 5\n",
    "for thread in threads:\n",
    "    thread.join()\n",
    "end = time()\n",
    "print('Took %.3f seconds' % (end - start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 6\n",
    "import select, socket\n",
    "\n",
    "# Creating the socket is specifically to support Windows. Windows can't do\n",
    "# a select call with an empty list.\n",
    "def slow_systemcall():\n",
    "    select.select([socket.socket()], [], [], 0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Took 0.515 seconds\n"
     ]
    }
   ],
   "source": [
    "# Example 7\n",
    "start = time()\n",
    "for _ in range(5):\n",
    "    slow_systemcall()\n",
    "end = time()\n",
    "print('Took %.3f seconds' % (end - start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 8\n",
    "start = time()\n",
    "threads = []\n",
    "for _ in range(5):\n",
    "    thread = Thread(target=slow_systemcall)\n",
    "    thread.start()\n",
    "    threads.append(thread)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Took 0.106 seconds\n"
     ]
    }
   ],
   "source": [
    "# Example 9\n",
    "def compute_helicopter_location(index):\n",
    "    pass\n",
    "\n",
    "for i in range(5):\n",
    "    compute_helicopter_location(i)\n",
    "for thread in threads:\n",
    "    thread.join()\n",
    "end = time()\n",
    "print('Took %.3f seconds' % (end - start))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
